package com.android.fastride.present;

import android.widget.Toast;

import com.android.fastride.R;
import com.android.fastride.contract.LoginContract;
import com.android.fastride.manager.DialogManager;
import com.android.fastride.manager.UserManager;
import com.android.fastride.model.http.entity.LoginUserInfoEntity;
import com.android.fastride.model.repository.ControlSystemRepository;
import com.peng.android.coremodule.utils.RxSchedulerUtils;
import com.peng.library.mantis.expand.data.MantisDataPresent;
import com.peng.library.mantis.mvp.PresentEvent;

import rx.Subscriber;

/**
 * 登录相关逻辑
 * Created by Martin on 2017/8/8.
 */

public class LoginPresent extends MantisDataPresent<LoginContract.VIEW, Boolean> implements LoginContract.PRESENT {

	private static final String TAG = "LoginPresent";



	@Override
	public void getVerfiyCode (String phoneNum, String deviceid) {
		/* final int dialogId = DialogManager.getInstance().showNotCancleLoadingDialog(getActivity(), getActivity().getString(R.string.please_wait), null);

		cn.ibenguo.controlsystem.model.repository.ControlSystemRepository.<String>getVerifyCode(phoneNum, deviceid)
				.compose(this.<String>bindUntilEvent(PresentEvent.DESTROY))
				.subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.compose(RxSchedulerUtils.normalSchedulersTransformer())
				.subscribe(
						NetworkUtils.createProxySubscriber(getActivity(), new Subscriber<Object>() {
									@Override
									public void onCompleted () {
									}

									@Override
									public void onError (Throwable e) {
										DialogManager.getInstance().dismissDialog(getActivity(),dialogId);
										e.printStackTrace();
										if (e instanceof ResponseException) {
											Toast.makeText(getActivity(), "获取验证码失败"+e.getMessage(), Toast.LENGTH_SHORT).show();
										} else {
											getViewDelegate().showErrorView("发生错误", e.toString());
										}
										getView().onGetVerifyCodeError();
									}

									@Override
									public void onNext (Object o) {
										DialogManager.getInstance().dismissDialog(getActivity(),dialogId);
										// 获取验证码成功
										Toast.makeText(getActivity(), "获取验证码成功", Toast.LENGTH_SHORT).show();
									}
								}
						)
				);
*/
	}

	@Override
	public void loginByVerifyCode (String phoneNum, String verfiycode, String deviceid) {/*
		final int dialogId = DialogManager.getInstance().showNotCancleLoadingDialog(getActivity(), getActivity().getString(R.string.please_wait), null);
		// 验证码登录
		cn.ibenguo.controlsystem.model.repository.ControlSystemRepository.<LoginUserInfoEntity>loginByVerifyCode(phoneNum, verfiycode, deviceid)
				.compose(RxSchedulerUtils.<LoginUserInfoEntity>normalSchedulersTransformer())
				.compose(this.<LoginUserInfoEntity>bindUntilEvent(PresentEvent.DESTROY))
				.subscribe(new Subscriber<LoginUserInfoEntity>() {
					@Override
					public void onCompleted () {

					}

					@Override
					public void onError (Throwable e) {
						DialogManager.getInstance().dismissDialog(getActivity(), dialogId);
						e.printStackTrace();
						Toast.makeText(getActivity(), "登录失败"+e.getMessage(), Toast.LENGTH_SHORT).show();
					}

					@Override
					public void onNext (LoginUserInfoEntity s) {
						DialogManager.getInstance().dismissDialog(getActivity(), dialogId);
						loginSuccess(s);
					}
				});*/
	}

	@Override
	public void loginByPassword (String phoneNum, String password, String deviceid) {
		final int dialogId = DialogManager.getInstance().showNotCancleLoadingDialog(getActivity(), getActivity().getString(R.string.please_wait), null);
		// 用户名密码登录
		// 验证码登录
		ControlSystemRepository.<String>loginByPwd(phoneNum, password, deviceid)
				.compose(RxSchedulerUtils.<LoginUserInfoEntity>normalSchedulersTransformer())
				.compose(this.<LoginUserInfoEntity>bindUntilEvent(PresentEvent.DESTROY))
				.subscribe(new Subscriber<LoginUserInfoEntity>() {
					@Override
					public void onCompleted () {
						// TODO: 测试跳转到主页
//						getView().skipToHomePage();
					}

					@Override
					public void onError (Throwable e) {
						DialogManager.getInstance().dismissDialog(getActivity(),dialogId);
						e.printStackTrace();
						String msg = e.getMessage();
						msg = e.getMessage().contains
								("Failed to connect")?"连接服务器失败":"请确认用户名和密码";
						Toast.makeText(getActivity(), "登录失败:"+ msg, Toast
								.LENGTH_SHORT).show();
					}

					@Override
					public void onNext (LoginUserInfoEntity s) {
						DialogManager.getInstance().dismissDialog(getActivity(),dialogId);
						// 设置登录密码
						s.setPassword(password);
						loginSuccess(s);
					}
				});

	}

	private void loginSuccess(LoginUserInfoEntity s) {
		Toast.makeText(getActivity(), "登录成功", Toast.LENGTH_SHORT).show();
		UserManager.getInstance().refreshUserInfo(getActivity(), s);
		getView().skipToHomePage();
	}

}
